﻿#region MIT Licence
/*
 * Copyright (C) 2014 PdfCreative
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace PdfCreative.Pdf.Graphics.Tables
{
    /// <summary>
    /// The MergeInformation class maintains information about the spanning of the cell. Cells may span more than
    /// one column, and more than one row. The cells in the range are said to have 'merged' into one cell.
    /// </summary>
    public class MergeInformation
    {
        // Private fields
        // ==============
        #region Instance fields
        private int _columns;
        private int _rows;
        #endregion



        // Public properties
        // =================
        #region Instance properties
        /// <summary>
        /// Sets or retrieves the number of columns that the cell spans.
        /// This must be a positive number.
        /// </summary>
        public int Columns
        {
            get { return _columns; }
            set {
                if (value <= 0)
                    throw new ArgumentOutOfRangeException(
                        "value", 
                        "Columns must be larger than zero. To indicate that a cell is hidden, use MergeInformation.Hidden");

                _columns = value; 
            }
        }

        /// <summary>
        /// Sets or retrieves the number of rows that the cell spans.
        /// This must be a positive number.
        /// </summary>
        public int Rows
        {
            get { return _rows; }
            set {
                if (value <= 0)
                    throw new ArgumentOutOfRangeException(
                        "value", 
                        "Rows must be larger than zero. To indicate that a cell is hidden, use MergeInformation.Hidden");

                _rows = value; 
            }
        }
        #endregion

        #region Static instances
        /// <summary>
        /// Returns a MergeInformation instance for a cell that does not span
        /// any rows or columns.
        /// </summary>
        public static MergeInformation NoMerge
        {
            get
            {
                return new MergeInformation(1,1);
            }
        }
        #endregion



        // Base class overrides
        // ====================
        #region Object overrides
        /// <summary>
        /// Returns the string representation of the instance.
        /// </summary>
        /// <returns>The string representation.</returns>
        public override string ToString()
        {
            return String.Format("Merge ({0},{1})", _columns, _rows);
        }

        /// <summary>
        /// Determines if this instance is equal to the other instance.
        /// </summary>
        /// <param name="obj">The other instance.</param>
        /// <returns>True if the number of columns and row spanned are equal for both
        /// instances; False otherwise.</returns>
        public override bool Equals(object obj)
        {
            MergeInformation other = obj as MergeInformation;
            
            return null!=other && 
                _columns.Equals(other._columns) && 
                _rows.Equals(other._rows);
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>The hash code.</returns>
        public override int GetHashCode()
        {
            return _columns + (_rows << 16);
        }
        #endregion



        // Public constructor
        // ==================
        #region Public constructor
        /// <summary>
        /// Constructs a new MergeInformation instance for a cell that is not merged; i.e.
        /// it does not span any additional rows or columns.
        /// </summary>
        public MergeInformation()
        : this(1,1) { }


        /// <summary>
        /// Constructs a new MergeInformation instance.
        /// </summary>
        /// <param name="columns">The number of columns that the cell spans.</param>
        /// <param name="rows">The number of rows that the cell spans.</param>
        public MergeInformation(int columns, int rows)
        {
            _columns = columns;
            _rows = rows;
        }
        #endregion

    } // MergeInformation class
}
